package com.leyou.item.service;

import com.gitee.yongzhuzl.commonutil.util.empty.EmptyUtil;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: 商品分类业务
 * @author 朱正磊
 * @date 2021-03-26 19:06:31
 */
@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    public List<Category> queryCategoryListByPid(Long pid) {
        // 构造查询条件，通用mapper会把对象中的非空属性作为查询条件
        Category category = new Category();
        category.setParentId(pid);
        List<Category> categoryList = categoryMapper.select(category);
        // 判断查询结果
        if (EmptyUtil.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categoryList;
    }

    /**
     * description: 根据主键集合查询商品分类集合
     * @author 朱正磊
     * @date 2021-04-01 23:12:00
     * @param ids 商品分类id集合
     * @return 返回商品分类集合
     */
    public List<Category> queryByIds(List<Long> ids) {
        List<Category> categoryList = categoryMapper.selectByIdList(ids);
        // 判断查询结果
        if (EmptyUtil.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categoryList;
    }

}
